package com.hexu.event.service;

import com.hexu.event.entity.User;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

import static com.hexu.event.util.DaoUtil.userDao;
import static com.hexu.event.util.WebUtil.show;

public class UserService {
    //检索是否存在一样的账号
    public boolean existAccount(String account){
        return userDao.existAccount(account);
    }

    //检索是否存在一样的用户名
    public boolean existUserName(String userName){
        return userDao.existUserName(userName);
    }

    //增加用户
    public boolean addUser(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
        //调用请求对象读取请求头参数信息，得到用户的信息信息
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        String userName = request.getParameter("userName");
        //new一个对象传入数据
        User user = new User(null,account,password,userName,0);
        //判断账号和用户名重复了没用
        if(!existAccount(account)){
            request.setAttribute("error","账号已存在");
            request.setAttribute("address","/hexu/UserRegister.html");
            request.getRequestDispatcher("/error.jsp").forward(request, response);
            return false;
        }else if(!existUserName(userName)){
            request.setAttribute("error","注用户名已存在");
            request.setAttribute("address","/hexu/UserRegister.html");
            request.getRequestDispatcher("/error.jsp").forward(request, response);
            return false;
        }
        //增加用户
        boolean flag = userDao.addUser(user);
        //添加成功

        show(flag, request, response,"注册成功","注册失败","/hexu/UserLeft.html","/hexu/UserRegister.html");
        return flag;
    }

    //读取单个用户信息
    public User getUser(String account){
        return  userDao.getUser(account);
    }

    //修改用户信息
    public boolean updateUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //调用请求对象读取请求头参数信息
        String account= request.getParameter("account");
        String password= request.getParameter("password");
        String userName= request.getParameter("userName");
        //判断用户名重复没有
        if(!existUserName(userName)){
            request.setAttribute("error","注用户名已存在");
            request.setAttribute("address","/hexu/user/find");
            request.getRequestDispatcher("/error.jsp").forward(request, response);
            return false;
        }else if(userName.equals("")){
            return false;
        }

        //调用请求对象，向tomcat索要当前用户在服务端私人储物柜
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        User user1 = new User(null,account,password,userName,user.getServiceWork());
        //改变session中的值
        session.setAttribute("user",user1);

        boolean flag = userDao.updateUser(user1);

        show(flag, request, response,"更新成功","更新失败","/hexu/user/find","/hexu/user/find");

        return  flag;

    }

    //成为服务工作者
    public boolean becomeServiceWorker(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //调用请求对象，向tomcat索要当前用户在服务端私人储物柜
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        user.setServiceWork(1);
        //改变session中的值
        session.setAttribute("user",user);
        boolean flag = userDao.updateUser(user);
        show(flag, request, response,"恭喜你成为一名服务工作者","很遗憾你未能成为一名服务工作者","/hexu/UserLeft.html","/hexu/UserLeft.html");
        return  flag;
    }

    //注销用户
    public boolean deleteUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //调用请求对象，向tomcat索要当前用户在服务端私人储物柜
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        boolean flag = userDao.deleteUser(user.getAccount());
        if(flag){
            //清空储物柜
            session.setAttribute("user",null);
        }

        show(flag, request, response,"注销成功","注销失败","/hexu/Login.html","/hexu/user/find");
        return  flag;

    }


}
